package com.nanyang.tms.api.dto.request.saleproduct.add;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiRequest;
import com.nanyang.common.core.exception.ValidateException;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Data
public class SaleProductBaseAddRequest extends ApiRequest {

    @JsonIgnore
    private String pin;
    @Valid
    @NotNull(message = "saleProductAddRequest不能为空")
    private SaleProductAddRequest saleProductAddRequest;
    @Valid
    @NotNull(message = "saleProductCarryRuleAddRequestList不能为空")
    private List<SaleProductCarryRuleAddRequest> saleProductCarryRuleAddRequestList;
    @Valid
    @NotNull(message = "saleProductWeightModelAddRequest不能为空")
    private SaleProductWeightModelAddRequest saleProductWeightModelAddRequest;
    @Override
    public void requestCheck() {
        //1、产品计重规则表 国家、城市、货物类型唯一；国家、城市为空时候设置未ALL，有城市信息 国家信息不能为空
        List<String> checkWeightModelList=new ArrayList<>();
        for (SaleProductWeightRuleAddRequest saleProductWeightRuleAddRequest : saleProductWeightModelAddRequest.getSaleProductWeightRuleAddRequestList()) {
            if(StringUtils.isNotEmpty(saleProductWeightRuleAddRequest.getCityCode())&&StringUtils.isEmpty(saleProductWeightRuleAddRequest.getCountyCode())){
                throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "计重规则中国家信息不能为空！");
            }
            if(StringUtils.isEmpty(saleProductWeightRuleAddRequest.getCityCode())){
                saleProductWeightRuleAddRequest.setCityCode("ALL");
                saleProductWeightRuleAddRequest.setCityName("ALL");
            }
            if(StringUtils.isEmpty(saleProductWeightRuleAddRequest.getCountyCode())){
                saleProductWeightRuleAddRequest.setCountyCode("ALL");
                saleProductWeightRuleAddRequest.setCountyName("ALL");
            }
            String weightModelStr=saleProductWeightRuleAddRequest.getCountyCode()+"-"+saleProductWeightRuleAddRequest.getCityCode()+"-"+saleProductWeightRuleAddRequest.getCargoTypeCode();
            if(checkWeightModelList.contains(weightModelStr)){
                throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "计重规则 国家、城市、货物类型信息唯一，不能存在重复");
            }
            checkWeightModelList.add(weightModelStr);
        }
        //2、计费重进位规则 起始重量和截止重量 区间唯一
        List<String> checkCarryRuleList=new ArrayList<>();
        for (SaleProductCarryRuleAddRequest saleProductCarryRuleAddRequest : saleProductCarryRuleAddRequestList) {
            String carryRuleStr=saleProductCarryRuleAddRequest.getWeightStart()+"-"+saleProductCarryRuleAddRequest.getWeightEnd();
            if(checkCarryRuleList.contains(carryRuleStr)){
                throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "计费重进位规则 起始重量和截止重量区间唯一，不能存在重复");
            }
            checkCarryRuleList.add(carryRuleStr);
        }
    }

}
